package practise.alogrithm;

import com.sun.org.apache.regexp.internal.recompile;
import org.compass.core.converter.basic.StringBuilderConverter;

import java.util.List;

/**
 * input two
 */
public class StringExcerise {
    /**
     * input AB, CD, out put, abcd, acdb, acdb....
     * @param a
     * @param b
     */
    public static void printAlterativeString(String a, String b,String result){
        if(a.isEmpty()){
            System.out.print(result + b);
            return;
        }
        if(b.isEmpty()){
            System.out.print(result + a);
            return;
        }

        printAlterativeString(a.substring(1),b, result+a.substring(0,1));
        printAlterativeString(a, b.substring(1), result + b.substring(0, 1));

    }

    public static int multiTwoString(String s1, String s2){
        int result=0;
        int level=10;

        for(int i =0; i<s1.length();i++){

            for(int j =0;j<s2.length();j++){
                result = ((level^j)*((s2.charAt(s2.length()-i-1)-'0')*
                        (s1.charAt(s2.length()-j-1)))*(level^i))+result;
            }
        }
        return result;
    }

    public static int convertStringToInt(String s){
        int result = 0;
        for(int i =0; i<s.length() ; i++){
            if(s.charAt(i)<'0'|| s.charAt(i)>9){
                break;
            }

            if(result==Integer.MAX_VALUE/10&&(s.charAt(i)-'0')>7 || result >Integer.MAX_VALUE/10){
                break;
            }
            else{
                result = result*10 + s.charAt(i)-'0';
            }
        }
        return result;
    }

    public List<String> getIpsFromInt(int value, String result, List<String> s){
        //todo validate the number
        if(value<0){
            //todo validate the string
           s.add(result);
            return s;
        }
        for(int temp=0;temp<=255;temp = value%10*10+value,value=value/10) {
            result = result+".";
            if(value/10==value)value=-1;
            getIpsFromInt(value/10,result, s) ;
            if(value==-1)break;
        }
        return s;
    }
}
